//
//  CALayer+Anim.m
//  CoreLock
//
//  Created by 冯成林 on 15/4/28.
//  Copyright (c) 2015年 冯成林. All rights reserved.
//

#import "CALayer+Anim.h"

@implementation CALayer (Anim)


/*
 摇动
 */
-(void)shake{
    
    CAKeyframeAnimation *kfa = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
    
    CGFloat s = 16;
    
    kfa.values = @[@(-s),@(0),@(s),@(0),@(-s),@(0),@(s),@(0)];
    
    //时长
    kfa.duration = .1f;
    
    //重复
    kfa.repeatCount =2;
    
    //移除
    kfa.removedOnCompletion = YES;
    
    [self addAnimation:kfa forKey:@"shake"];
}


/**
 缩放
 */
- (void)scale{
    
    
    CAKeyframeAnimation *kfa = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    kfa.duration = .3f;
    
    kfa.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 0.8)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.1)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    //移除
    kfa.removedOnCompletion = YES;
    
    [self addAnimation:kfa forKey:@"scale"];
    
}


/**
 翻滚
 */
- (void)roll{
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    CATransform3D trans = CATransform3DIdentity;
    animation.fromValue = [NSValue valueWithCATransform3D:trans];
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(trans, M_PI, 1, 0, 0)];
    animation.duration = 0.3;
    animation.removedOnCompletion = YES;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [self addAnimation:animation forKey:@"roll"];
    
    /*
    CATransition *animation = [CATransition animation];
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.fillMode = kCAFillModeForwards;
    animation.type = @"oglFlip";
    animation.subtype = kCATransitionFromBottom;
    animation.duration = 0.3;
    [self addAnimation:animation forKey:@"RollTransition"];
     */
}


/**
 转场渐变
 */
- (void)fade{
    
    if (![self animationForKey:@"FadeTransition"]) {
        CATransition *animation = [CATransition animation];
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;
        animation.type = kCATransitionFade;
        animation.duration = 0.3;
        [self addAnimation:animation forKey:@"FadeTransition"];
    }
}

/**
 转场Push
 */
- (void)push{
    
    if (![self animationForKey:@"PushTransition"]) {
        CATransition *animation = [CATransition animation];
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;
        animation.type = kCATransitionPush;
        animation.subtype = kCATransitionFromRight;
        animation.duration = 0.3;
        [self addAnimation:animation forKey:@"PushTransition"];
    }
}

/**
 转场覆盖
 */
- (void)moveIn{
    
    if (![self animationForKey:@"MoveInTransition"]) {
        CATransition *animation = [CATransition animation];
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;
        animation.type = kCATransitionMoveIn;
        animation.subtype = kCATransitionFromRight;
        animation.duration = 0.3;
        [self addAnimation:animation forKey:@"MoveInTransition"];
    }
}

/**
 转场揭开
 */
- (void)reveal{
    
    if (![self animationForKey:@"RevealTransition"]) {
        CATransition *animation = [CATransition animation];
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;
        animation.type = kCATransitionReveal;
        animation.subtype = kCATransitionFromRight;
        animation.duration = 0.3;
        [self addAnimation:animation forKey:@"RevealTransition"];
    }
}

@end
